<?php

namespace addons\xapply\controller;

use addons\xapply\Base;
use app\admin\model\department\Admin;
use app\admin\model\department\Department;
use app\common\model\User;

class Contact extends Base
{

    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /**
     * 部门与成员
     * url: http://域名/addons/xapply/contact/users
     * doc: http://lutui.net/doc/app/contacts.html
     */
    public function users()
    {
        $id = $this->data['id']; // 部门ID
        $dp = Department::where('parent_id', $id)->select();
        $data = [];
        foreach ($dp as $value){
            $data[] = [
                'id' => intval($value['id']),
                'name' => $value['name'],
                'dept' => 1,
                'icon' => '',
            ];
        }
        $object = new User();
        $object = $object->where('status', 'normal');
        if ($id > 0){
            $dpa = Admin::where('is_owner', 1)->where('department_id', 'in', Department::getChildrenIds($id, true))->column('admin_id');
            $object = $object->where('id', 'in', $dpa);
        }
        $arr = $object->select();
        foreach ($arr as $value){
            $data[] = [
                'id' => intval($value['id']),
                'name' => $value['nickname'],
                'phone' => $value['mobile'],
                'url' => $value['avatar'],
            ];
        }
        return json($data);
    }
}
